//
//  XPCrashLogUtils.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2018/4/16.
//  Copyright © 2018年 Ampaw. All rights reserved.
//

#import "XPCrashLogUtils.h"

#define xiaoPengCrashLogPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"XiaoPeng@CrashLog"]

@implementation XPCrashLogUtils

/**
 存储异常数据
 
 @param data 异常数据 {"time":urlencode(timestamp),"msg":urlencode(exceptionJsonString)}
 */
+ (void)saveCrashLogWithJSONData:(NSDictionary *)data
{
    @try {
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:xiaoPengCrashLogPath];
        NSMutableArray *crashLogs = [NSMutableArray arrayWithArray:array];
        [crashLogs addObject:data];
        
        [NSKeyedArchiver archiveRootObject:crashLogs toFile:xiaoPengCrashLogPath];
    } @catch (NSException *exception) {
        NSLog(@"Exception information storage failure !");
    } @finally {
        
    }
}

/**
 删除已上报数据
 */
+ (void)deleteCrashLog
{
    @try {
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:xiaoPengCrashLogPath];
        NSMutableArray *crashLogs = [NSMutableArray arrayWithArray:array];
        [crashLogs removeAllObjects];
        
        [NSKeyedArchiver archiveRootObject:crashLogs toFile:xiaoPengCrashLogPath];
    } @catch (NSException *exception) {
        NSLog(@"Exception information deletion failure !");
    } @finally {
        
    }
}

/**
 读取存储异常数据
 
 @return 异常数据数组
 */
+ (NSArray *)readCrashData
{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:xiaoPengCrashLogPath];
}

@end
